package com.tvt.sexyapp_demo.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.tvt.sexyapp_demo.MainActivity;
import com.tvt.sexyapp_demo.R;
import com.tvt.sexyapp_demo.appconfig.manager.PageInfoManager;
import com.tvt.sexyapp_demo.appconfig.xml.XItem;
import com.tvt.sexyapp_demo.appconfig.xml.XPopupConfirm;
import com.tvt.sexyapp_demo.callback.BaseUpdateCallback;
import com.tvt.sexyapp_demo.downloader.FileDownloaderUseAsyncTask;
import com.tvt.sexyapp_demo.fragment.dialog.AppInfoDialog;
import com.tvt.sexyapp_demo.fragment.dialog.LoadingDialog;
import com.tvt.sexyapp_demo.fragment.dialog.SMSDialog;
import com.tvt.sexyapp_demo.page.LocalPageController;
import com.tvt.sexyapp_demo.sms.SmsSender;

public class MyListFragment extends Fragment implements OnClickListener, OnItemClickListener {

	// ===================================================================
	// Constants
	// ===================================================================
	private final String DIALOG_TAG = "sms_dialog";

	// ===================================================================
	// Fields
	// ===================================================================
	private MyAdapter mAdapter;
	private ListView mListView;
	private ArrayList<XItem> mXItems;
	// private AppConfigXml mAppConfig;
	private MainActivity mActivity;

	// ===================================================================
	// Constructors
	// ===================================================================
	// public MyListFragment(MainActivity pActivity) {
	// super();
	// this.mActivity = pActivity;
	// }

	public void setActivity(MainActivity pActivity) {
		this.mActivity = pActivity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.flagment_list, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {

		this.mListView = (ListView) view.findViewById(R.id.listView);

		this.mXItems = PageInfoManager.getInstance().getItemsOnPage();

		final LoadingDialog loadingDialog = new LoadingDialog();
		BaseUpdateCallback updateCallback = new BaseUpdateCallback() {

			@Override
			public void run() {

				MyListFragment.this.mXItems = this.getXItemsOfPage();

				MyListFragment.this.mAdapter = new MyAdapter(getActivity().getApplicationContext(),
						MyListFragment.this.mXItems);

				addFooter();

				MyListFragment.this.mListView.setAdapter(MyListFragment.this.mAdapter);

				MyListFragment.this.mListView.setOnItemClickListener(MyListFragment.this);
			}

			@Override
			public void showLoading() {
				loadingDialog.show(getFragmentManager(), "abc");
			}

			@Override
			public void dismissLoading() {
				loadingDialog.dismiss();
			}
		};
		new FileDownloaderUseAsyncTask(updateCallback).execute(this.mXItems);

		// this.mAdapter = new
		// MyAdapter(this.getActivity().getApplicationContext(), this.mXItems);
		// this.addFooter();
		// this.mListView.setAdapter(this.mAdapter);
		// this.mListView.setOnItemClickListener(this);

		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnInfo:
			AppInfoDialog infoDialog = new AppInfoDialog();
			infoDialog.show(getFragmentManager(), DIALOG_TAG);
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		if (PageInfoManager.getInstance().getAppConfig().getRunningApp().isDisplayConfirmPopup()) {
			XPopupConfirm xPopupConfirm = PageInfoManager.getInstance().getAppConfig().getRunningApp()
					.getPopupConfirm();
			SMSDialog dialog = new SMSDialog(this.mXItems.get(position), xPopupConfirm);
			dialog.show(getFragmentManager(), DIALOG_TAG);
		} else {
			SmsSender.sendTextSMS(PageInfoManager.getInstance().getAppConfig().getRunningApp().getItems()
					.get(position).getSms());
		}
	}

	// ===================================================================
	// Methods
	// ===================================================================
	private void addFooter() {

		LayoutInflater inflater = (LayoutInflater) this.getActivity().getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View footerView = inflater.inflate(R.layout.layout_footer, this.mListView, false);
		this.createLocalPageController(footerView);

		this.mListView.addFooterView(footerView);
	}

	private void createLocalPageController(View pFooterView) {

		// Button btnPager1 = (Button) pFooterView.findViewById(R.id.btnPager1);
		// Button btnPager2 = (Button) pFooterView.findViewById(R.id.btnPager2);
		// Button btnPager3 = (Button) pFooterView.findViewById(R.id.btnPager3);
		//
		// Button btnNext = (Button) pFooterView.findViewById(R.id.btnNext);
		// Button btnPrevious = (Button) pFooterView.findViewById(R.id.btnNext);
		//
		// new LocalPageController(this, btnPrevious, btnNext, btnPager1,
		// btnPager2, btnPager3);

		Button btnInfo = (Button) pFooterView.findViewById(R.id.btnInfo);
		btnInfo.setOnClickListener(this);
	}

	public void refreshPage() {
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		MyListFragment fragment = new MyListFragment();
		fragment.setActivity(mActivity);
		fragmentTransaction.replace(R.id.listFragment, fragment);
		fragmentTransaction.commit();
	}

}
